﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using System.Xml.Serialization;
using System.IO;

namespace Fighter.GameContent
{
    /// <summary>
    /// Classe permettant de charger la configuration du joueur
    /// </summary>
    public class Config
    {
        // Constantes en cas de suppression du fichier de configuration
        public const Keys JUMP = Keys.Space;
        public const Keys UP = Keys.Z;
        public const Keys DOWN = Keys.S;
        public const Keys RIGHT = Keys.D;
        public const Keys LEFT = Keys.Q;
        public const Keys FIRE = Keys.F;
        public const Keys ATTACK = Keys.R;
        public const bool MUSIC = true;

        // Instance unique pour la configuration
        private static Config instance;

        // Variables configurables
        private Keys jump;
        private Keys up;
        private Keys down;
        private Keys right;
        private Keys left;
        private Keys fire;
        private Keys attack;
        private bool music;

        public Keys Jump
        {
            get { return jump; }
            set { jump = value; }
        }
        public Keys Attack
        {
            get { return attack; }
            set { attack = value; }
        }

        public Keys Up
        {
            get { return up; }
            set { up = value; }
        }

        public Keys Down
        {
            get { return down; }
            set { down = value; }
        }

        public Keys Right
        {
            get { return right; }
            set { right = value; }
        }

        public Keys Left
        {
            get { return left; }
            set { left = value; }
        }

        public Keys Fire
        {
            get { return fire; }
            set { fire = value; }
        }

        public bool Music
        {
            get { return music; }
            set { music = value; }
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        private Config()
        {
        }

        /// <summary>
        /// Permet de créer une instance unique pour la configuration
        /// </summary>
        public static Config Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Config();
                    LoadConfig();
                }
                return instance;
            }
        }

        /// <summary>
        /// Méthode permettant de charger la configuration du joueur
        /// </summary>
        private static void LoadConfig()
        {
            if (File.Exists("Content/Game/config.xml"))
            {
                // Chargement depuis le fichier
                XmlSerializer serializer = new XmlSerializer(instance.GetType());
                TextReader tr = new StreamReader("Content/Game/config.xml");
                Config config = (Config)serializer.Deserialize(tr);
                tr.Close();
                instance.jump = config.jump;
                instance.up = config.up;
                instance.down = config.down;
                instance.right = config.right;
                instance.left = config.left;
                instance.fire = config.fire;
                instance.attack = config.attack;
                instance.music = config.music;
            }
            else
            {
                // Sinon on crée le fichier
                instance.jump = JUMP;
                instance.up = UP;
                instance.down = DOWN;
                instance.right = RIGHT;
                instance.left = LEFT;
                instance.fire = FIRE;
                instance.attack = ATTACK;
                instance.music = MUSIC;
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(instance.GetType());
                TextWriter tw = new StreamWriter("Content/Game/config.xml");
                x.Serialize(tw, instance);
                tw.Close();
            }
        }
    }
}
